package cn.kgm.makeGodV2;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Version 1.8
 * @Author: Kappi
 * @Date: 2025-9-16 14:11
 * @Description: cn.kgm.makeGodV2.UnifiedTagManager
 */
@Data
@Builder
@AllArgsConstructor
public class UnifiedTagManager {
    // 1. 特质标签 (String: tagKey)
    private Set<String> traitTags;

    // 2. 状态标签 (String: tagKey -> TagMetadata)
    private Map<String, TagMetaData> stateTags;

    // 3. 关系标签 (String: tagType -> Map<String: targetId, Double: strength>)
    private Map<String, Map<String, Double>> relationshipTags;

    public UnifiedTagManager() {
        traitTags = new HashSet<>();
        stateTags = new HashMap<>();
        relationshipTags = new HashMap<>();
    }

    // --- 通用方法 ---
    public boolean hasTag(String tagKey) {
        // 检查所有类别的标签
        return traitTags.contains(tagKey) || stateTags.containsKey(tagKey);
    }

    // 更复杂的查询：检查关系标签
    public boolean hasRelationship(String relationType, String targetId, double minStrength) {
        Map<String, Double> relationsOfType = relationshipTags.get(relationType);
        if (relationsOfType == null) return false;
        Double strength = relationsOfType.get(targetId);
        return strength != null && strength >= minStrength;
    }

    // --- 特质标签管理 ---
    public void addTraitTag(String tag) {
        traitTags.add(tag);
    }

    public boolean removeTraitTag(String tag) {
        return traitTags.remove(tag);
    }

    // --- 状态标签管理 ---
    public void addStateTag(String tag, TagMetaData metadata) {
        stateTags.put(tag, metadata);
    }

    public void removeStateTag(String tag) {
        stateTags.remove(tag);
    }

    // 定期清理过期状态标签
    public void cleanupExpiredTags(long currentTime) {
        stateTags.entrySet().removeIf(entry -> entry.getValue().isExpired(currentTime));
    }

    // --- 关系标签管理 ---
    public void setRelationship(String relationType, String targetId, double strength) {
        Map<String, Double> relations = relationshipTags.computeIfAbsent(relationType, k -> new HashMap<>());
        relations.put(targetId, strength);
        // 可选：如果强度为0，则移除该关系
        if (strength == 0) {
            relations.remove(targetId);
        }
    }

    public double getRelationshipStrength(String relationType, String targetId) {
        Map<String, Double> relations = relationshipTags.get(relationType);
        if (relations == null) return 0;
        return relations.getOrDefault(targetId, 0.0);
    }
}
